<template>
<Tabs :value="tabName" class="p-workOrderTest">
<TabPane label="全局配置" name="1">
    <Card class="ivu-mb" dis-hover >
    <Form :labelWidth="120" :model="startRouting.formData">
        <Row>
        <Col v-for="(item,index) in startRouting.fieldList" :key="index" :span="item.span || 24">
            <FieldItem
            v-model="startRouting.formData[item.key]" :config="item.config"
            :formItemProps="{label: item.title, labelWidth: item.labelWidth, prop: item.key, }"
            />
        </Col>
        </Row>
        <Button @click="onStartRouting" type="primary" ghost>启动工艺路线</Button>
    </Form>
    </Card>
</TabPane>

<TabPane label="新建实例并link到节点" name="2">
    <Card class="ivu-mb" dis-hover title="创建产品">
    <Form :labelWidth="100" :model="createProductAndLink">
        <Row>
        <Col v-for="(item,index) in createProductAndLink.fieldList" :key="index" :span="item.span || 24">
            <FieldItem
                v-model="createProductAndLink.formData[item.key]" :config="item.config"
                :formItemProps="{label: item.title, prop: item.key, }"
            />
        </Col>
        </Row>
        <Button @click="onCreateProductAndLink" type="primary" ghost>新建产品</Button>
    </Form>
    </Card>
    
    <Card class="ivu-mb" dis-hover title="创建报工单">
    <Form :labelWidth="100" :model="createWorkAndLink">
        <Row>
        <Col v-for="(item,index) in createWorkAndLink.fieldList" :key="index" :span="item.span || 24">
            <FieldItem
                v-model="createWorkAndLink.formData[item.key]" :config="item.config"
                :formItemProps="{label: item.title, prop: item.key, }"
            />
        </Col>
        </Row>
        <Button @click="onCreateWorkAndLink" type="primary" ghost>新建报工单</Button>
    </Form>
    </Card>
</TabPane>

<TabPane label="报工" name="3">
    <Form :labelWidth="100">
        <Row class="flex-cm"><Col flex="1">
        <FieldItem
            v-model="workReportInstance" :config="{
                type: 'pointer', pointerName: 'container', props: {isInstance: true, type: 'workCenter', prefix: 'workCenter'}
            }"
            :formItemProps="{label: 'nodeId', prop: 'workReportInstance.nodeId', }"
        /></Col>
        <Col ><Button @click="onQueryWorkReport" type="primary" ghost style="margin: 12px;">查询未完成的报工单</Button></Col></Row>
    </Form>
    <Row>
        <Col flex="150px">
            <Menu @on-select="menuChange" :active-name="curMenuIndex">
                <MenuItem v-for="(item,index) in menuData" :key="index" :name="index">{{item.name}}</MenuItem>
            </Menu>
        </Col>

        <Col flex="1" v-show="curMenuIndex >= 0">
        <Card title="报工表单" class="ivu-mt" dis-hover>
        <Form :labelWidth="100" :model="reportWorkData">
            <Row>
            <Col v-for="(item,index) in reportWork.fieldList" :key="index" :span="item.span || 12">
                <FieldItem
                v-model="reportWorkData[item.key]" :config="item.config"
                :formItemProps="{label: item.title, prop: item.key, }"
                />
            </Col>
            </Row>
            <Button @click="onReportWork" type="primary" ghost>报工</Button>
        </Form></Card>

        <Card title="完工表单" class="ivu-mt" dis-hover>
        <Form :labelWidth="100" :model="finishWork.formData">
            <Row>
            <Col v-for="(item,index) in finishWork.fieldList" :key="index" :span="item.span || 12">
                <FieldItem
                v-model="finishWork.formData[item.key]" :config="item.config"
                :formItemProps="{label: item.title, prop: item.key, }"
                />
            </Col>
            </Row>
            <Button @click="onFinishWork" type="primary" ghost>完工报工</Button>
        </Form></Card>
        </Col>
    </Row>
</TabPane>
</Tabs>
</template>

<script>
export default {
    components: {  },
    data() {
        return {
            tabName: '3',
            reportWorkInstanceId: 222,
            classObject: null,
            modelFieldList: [],
            menuData: [
                {nodeId: 'ab097dea154c4bb9bc9eb6a47c6528a1', name: '工单1', count: 12},
                {nodeId: '88b2bac6441c4d8284cf38989f77895a', name: '工单2', count: 26},
            ],
            curMenuIndex: -1,
            startRouting: {
                formData: {
                    linkRouting: {
                        linkType: 'routing', linkClassName: '生产料片2'
                    },
                    linkWorkCenter: {
                        linkType: 'workCenter', linkClassName: '落料工位'
                    },
                },
                fieldList: [
                    {
                        key: 'linkRouting',
                        title: '绑定工艺路线',
                        config: {
                            type: 'pointer',
                            pointerName: 'container',
                            props: {
                                type: 'routing',
                                prefix: 'routing'
                            }
                        },
                    },
                    {
                        key: 'linkWorkCenter',
                        title: '绑定工作中心',
                        config: {
                            type: 'pointer',
                            pointerName: 'container',
                            props: {
                                type: 'workCenter',
                                prefix: 'workCenter'
                            }
                        },
                    },
                ]
            },
            createProductAndLink: {
                formData: {
                    createInsNode: {
                        linkType: 'workCenter',
                        linkClassName: '落料工位',
                        nodeId: '21qm8lvnp6f400',
                    },
                    name: '料片箱' + (+ new Date()),
                    '产品名称': '料片箱1'
                },
                fieldList: [
                    // createInsNodeId: 1, // 原始的节点nodeId
                    // enterNodeId: 1,     // 创建后需要添加子节点的父节点nodeId
                    {
                        key: 'createInsNode',
                        title: '实例nodeId',
                        config: {
                            type: 'pointer',
                            pointerName: 'container',
                            props: {
                                isInstance: true,
                                type: 'workCenter',
                                prefix: 'workCenter'
                            }
                        },
                    },
                    {
                        key: 'name',
                        title: '实例名',
                        span: 12,
                        config: {
                            type: 'text', clearable: true
                        },
                    },
                    {
                        key: '产品名称',
                        title: '产品名称',
                        span: 12,
                        config: {
                            type: 'text', clearable: true,
                            // options:[ {label: '料片', value: '料片'}, ]
                        },
                    },
                ]
            },
            createWorkAndLink: {
                formData: {
                    createInsNode: {
                        linkType: 'workCenter',
                        linkClassName: '落料工位',
                        nodeId: '2o5xa22sdmm000',
                    },
                    name: '报工单' + (+ new Date()),
                },
                fieldList: [
                    // createInsNodeId: 1, // 原始的节点nodeId
                    // enterNodeId: 1,     // 创建后需要添加子节点的父节点nodeId
                    {
                        key: 'createInsNode',
                        title: '报工单实例nodeId',
                        config: {
                            type: 'pointer',
                            pointerName: 'container',
                            props: {
                                isInstance: true,
                                type: 'workCenter',
                                prefix: 'workCenter'
                            }
                        },
                    },
                    {
                        key: 'name',
                        title: '实例名',
                        span: 12,
                        config: {
                            type: 'text', clearable: true
                        },
                    },
                ]
            },
            reportWork: {
                fieldList: [
                    {
                        key: 'linkEvent',
                        title: '关联事件',
                        span: 24,
                        config: { type: 'pointer', pointerName: 'event', },
                    },
                    {
                        key: 'linkProduct',
                        title: '关联产品',
                        span: 24,
                        config: {
                            type: 'pointer', 
                            pointerName: 'container',
                            props: {
                                isInstance: true,
                                type: 'workCenter',
                                prefix: 'workCenter'
                            }
                        },
                    },
                    // {
                    //     key: 'name',
                    //     title: '实例名称',
                    //     span: 12,
                    //     config: {
                    //         type: 'text', clearable: true,
                    //     },
                    // },
                    {
                        key: 'auth',
                        title: '报工人',
                        span: 12,
                        config: {
                            type: 'text', clearable: true
                        },
                    },
                    {
                        key: 'count',
                        title: '报工数量',
                        span: 12,
                        config: {
                            type: 'number', clearable: true
                        },
                    },
                ]
            },
            finishWork: {
                formData: {
                    linkEvent: {
                        // category: '分类', name: '改变workCenter状态'
                    },
                },
                fieldList: [
                    {
                        key: 'linkEvent',
                        title: '关联事件',
                        span: 24,
                        config: { type: 'pointer', pointerName: 'event', },
                    },
                ]
            },
            wcId: 1,
            routingId: 1,
            workReportInstance: {
                linkType: 'workCenter',
                linkClassName: '落料工位',
                nodeId: '2o5xa22sdmm000',
            },
        };
    },
    async beforeMount() {
        this.classObject = this.$modelObject.create('logic')
        this.collectionModelObject = this.$modelObject.create(`logicModelCollection`)
        this.collectionModelObject.query("type='logic'").then(flag => {
            let flag1 = this.classObject.query("type='logic' and id=" + this.reportWorkInstanceId)

            this.modelFieldList = this.classObject.calcModelFields(this.collectionModelObject.records)
            // console.log("file: index.vue:170  this.collectionModelObject", this.modelFieldList)
        })
        this.menuChange(0)
        this.onQueryWorkReport()
    },
    mounted() {
    },
    methods: {
        menuChange(index) {            
            let finishWorkDefault = {
                linkEvent: {
                    category: '工作中心1', name: '改变workCenter状态'
                },
                auth: '员工1'
            }
            this.curMenuIndex = index
            this.finishWork.formData = {...finishWorkDefault, ...this.menuData[this.curMenuIndex]}
        },
        // 1. 启动工艺路线，生成工艺路线实例、工作中心实例
        async onStartRouting() {
            let data = this.startRouting.formData
            let res = await this.$api.RoutingInstance.startRouting({
                workCenterClassName: data.linkWorkCenter.linkClassName,
                routingClassName: data.linkRouting.linkClassName
            })
            console.log("file: index.vue:278  onStartRouting  res:", res)
            this.wcId = res.wcInstanceId
            this.routingId = res.routingInstanceId
        },
        // 2. 新建实例并link到节点
        async onCreateProductAndLink() {
            let data = this.createProductAndLink.formData
            let initData = this.startRouting.formData
            let res = await this.$api.business.eventExecute({
                category: '工作中心1', name: '创建实例并链接到指定容器',
                data: {
                    params: [
                        { name: 'wcClassName', value: initData.linkWorkCenter.linkClassName },
                        { name: 'wcId', value: this.wcId},
                        { name: 'createInsNodeId', value: data.createInsNode.nodeId },
                        { name: 'enterNodeId', value: data.createInsNode.nodeId },
                        { name: 'name', value: data.name },
                        { name: 'values', value: { 产品名称: data['产品名称'] } },
                    ],
                    back: ['container', 'newRec']
                }
            })
            console.log("file: index.vue:280  onCreateReportWork  res:", data, res)
        },
        async onCreateWorkAndLink() {
            let data = this.createWorkAndLink.formData
            let initData = this.startRouting.formData
            let res = await this.$api.business.eventExecute({
                category: '工作中心1', name: '创建实例并链接到指定容器',
                data: {
                    params: [
                        { name: 'wcClassName', value: initData.linkWorkCenter.linkClassName },
                        { name: 'wcId', value: this.wcId},
                        { name: 'createInsNodeId', value: data.createInsNode.nodeId },
                        { name: 'enterNodeId', value: data.createInsNode.nodeId },
                        { name: 'name', value: data.name },
                        // { name: 'values', value: {} },
                    ],
                    back: ['container', 'newRec']
                }
            })
            console.log("file: index.vue:388  onCreateWorkAndLink  res:", res)
        },
        // 查询未完工的报工单
        async onQueryWorkReport() {
            let res = await this.$api.WorkCenterInstance.queryAllByParentNodeId({
                // "action": "WORK_CENTER_INSTANCE_QUERY_ALL_BY_PARENT_NODE_ID",
                instanceId: this.wcId,
                nodeId: this.workReportInstance.nodeId,
                status: "1"
            })
            if (res && res.content) {
                this.menuData = res.content
                this.menuData.length && this.menuChange(0)
            }
        },
        // 报工
        async onReportWork() {
            let data = this.reportWorkData;
            let initData = this.startRouting.formData
            let productNodeId = data.linkProduct.nodeId || '4b7ba990eec3466098c7adb934c2d3cf'

            console.log("file: index.vue:393  onReportWork  data:", data)
            let res = this.$api.business.eventExecute({
                category: data.linkEvent.category,
                name: data.linkEvent.name,
                data: {
                    params: [
                        {name: 'wcClassName', value: initData.linkWorkCenter.linkClassName},
                        {name: 'wcId', value: this.wcId},
                        {name: 'wcNodeId', value: data.nodeId},  // 报工单nodeId
                        {name: 'transferInsNodeId', value: productNodeId}, // 产品nodeId
                        // 移入方式1：只有是工作中心才填写
                        // {name: 'enterNodeId', value: data.enterLink.nodeId},
                        // 移入方式2：非工作中心
                        {name: 'enterModel', value: 'tank'},
                        {name: 'enterClassName', value: '料片大箱class'},
                        {name: 'enterContainerId', value: 204},
                        {name: 'status', value: '2'},
                        {name: 'occupyStatus', value: '2'},
                        {name: 'wcNodeList', value: productNodeId},
                        {
                            "name": "instanceValues",
                            "value": {
                                "wcNodeList": productNodeId,
                                '报工人': data.auth,
                                '报工数量': data.count
                            }
                        },
                    ],
                    back: []
                }
            })
        },
        // 完工报工
        async onFinishWork() {
            let data = this.finishWork.formData
            
            let res = this.$api.business.eventExecute({
                category: data.linkEvent.category,
                name: data.linkEvent.name,
                data: {
                    params: [
                        {name: 'routingId', value: this.routingId}, // 实例id
                        {name: 'wcId', value: this.wcId},   // 实例id
                        {name: 'status', value: '3'},
                    ],
                    back: []
                }
            })
        },
    },
    computed: {
        reportWorkData() {
            let reportWorkDefault = {
                linkEvent: {
                    category: '工作中心1', name: '中间报工'
                },
                linkProduct: {},
                auth: '员工1'
            };
            let res = this.menuData.length ? {...reportWorkDefault, ...this.menuData[this.curMenuIndex]} : reportWorkDefault
            console.log("file: index.vue:433  reportWorkData  res:", res)
            return res
        },
        finishWorkData() {
        }
    }
};
</script>

<style lang="less">
.p-workOrderTest {
    padding: 0 16px 16px;
    background-color: #fff;
    .ivu-btn {
        margin-top: 16px;
    }
    .c-formItem {
        margin-bottom: 4px;
    }
    .btnwp {
        display: flex;
        justify-content: space-between;
    }
}
</style>
